<?php
namespace Service;

class Order extends Base
{

    public function getDatabase()
    {
        return $this->get(DATABASE_ORDER);
    }

    public function getByOrderNo($orderNo)
    {
        $database = $this->getDatabase();
        return $database->getOneByField('dry_order_no', $orderNo);
    }

    public function getList($userId, $statusIn, $page, $limit)
    {
        $database = $this->getDatabase();
        return $database->getList($userId, $statusIn, $page, $limit);
    }

    public function getCount($userId)
    {
        $database = $this->getDatabase();
        return $database->getCount($userId);
    }

    public function canDownload($userId, $order, $product)
    {
        $map = getMainProductSpec();
        $mainProduct = $product->dry_main_product;
        $mainProductSpec = $product->dry_main_product_spec;
        $now = getTimestamp();
        $orderTime = strtotime($order->dry_add_time);
        $bool1 = $order->dry_user == $userId;
        $bool2 = in_array($order->dry_status, [ORDER_STATUS_PAY, ORDER_STATUS_FINISH]);
        $bool3 = $mainProduct == 'drycms';
        $bool4 = isset($map[$mainProductSpec]) && $now <= $orderTime + $map[$mainProductSpec];
        if($bool1 && $bool2 && $bool3 && $bool4){
            return date('Y-m-d H:i:s', $orderTime + $map[$mainProductSpec]);
        }
        return '0';
    }

    public function isBuy($userId, $productId)
    {
        $database = $this->getDatabase();
        return $database->isBuy($userId, $productId);
    }

}